package com.huawei.esdk.tp.business.professional.confctrl;

import java.util.List;
import com.huawei.esdk.platform.common.SDKErrorCode;
import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.config.ConfigManager;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.platform.common.utils.ListUtils;
import com.huawei.esdk.platform.common.utils.StringUtils;
import com.huawei.esdk.tp.base.BaseService;
import com.huawei.esdk.tp.common.ErrInfo;
import com.huawei.esdk.tp.domain.model.HoldingConference;
import com.huawei.esdk.tp.domain.model.RecordingConference;
import com.huawei.esdk.tp.domain.model.SiteInConference;
import com.huawei.esdk.tp.domain.model.SiteInfoCommService;
import com.huawei.esdk.tp.domain.model.bean.ContinuousPresenceCapability;
import com.huawei.esdk.tp.domain.model.bean.ContinuousPresenceParam;
import com.huawei.esdk.tp.domain.model.bean.SiteVolume;

public class ConfCtrlService extends BaseService
{
 
    private SiteInfoCommService siteInfoCommService;
    
    public SiteInfoCommService getSiteInfoCommService()
    {
        return siteInfoCommService;
    }
    
    public void setSiteInfoCommService(SiteInfoCommService siteInfoCommService)
    {
        this.siteInfoCommService = siteInfoCommService;
    }
    
    public SDKErrorCode setVideoSourceEx(String confId, String siteUri, String videoSourceUri, Integer isLock)
        throws SDKException
    {
        SDKErrorCode errorCode = new SDKErrorCode();
        if (StringUtils.isEmpty(confId))
        {
            errorCode.setErrCode(ErrInfo.CONF_ID_ISNULL_ERRORCODE);
            return errorCode;
        }
        if (!StringUtils.isUintNumber(confId))
        {
            errorCode.setErrCode(ErrInfo.CONF_ID_IS_INVALID_ERRORCODE);
            return errorCode;
        }
        if (StringUtils.isEmpty(siteUri) || StringUtils.isEmpty(videoSourceUri))
        {
            errorCode.setErrCode(ErrInfo.SITE_URI_ISNULL_ERRORCODE);
            return errorCode;
        }
        Boolean isLck;
        if (null != isLock && 1 == isLock)
        {
            isLck = true;
        }
        else if (null != isLock && 0 == isLock)
        {
            isLck = false;
        }
        else
        {
            errorCode.setErrCode(ErrInfo.DATA_ERRORCODE);
            return errorCode;
        }
        HoldingConference holdingConference = new HoldingConference(confId);
        errorCode = holdingConference.setVideoSourceOfSite(siteUri, videoSourceUri, isLck);
        
        return errorCode;
    }
    
    public SDKErrorCode setAudioSwitchEx(String confId, Integer isSwitch, Integer swtichGate)
        throws SDKException
    {
        SDKErrorCode result = new SDKErrorCode();
        boolean enable = false;
        if (StringUtils.isEmpty(confId))
        {
            result.setErrCode(ErrInfo.CONF_ID_ISNULL_ERRORCODE);
            return result;
        }
        if (!StringUtils.isUintNumber(confId))
        {
            result.setErrCode(ErrInfo.CONF_ID_IS_INVALID_ERRORCODE);
            return result;
        }
        
        if (null == swtichGate)
        {
            swtichGate =
                Integer.parseInt(ConfigManager.getInstance()
                    .getValue("professional.setAudioSwitch.switchGateDefaultVal"));
        }
        
        int switchGateMaxVal =
            Integer.parseInt(ConfigManager.getInstance().getValue("professional.setAudioSwitch.switchGateMaxVal"));
        int switchGateMinVal =
            Integer.parseInt(ConfigManager.getInstance().getValue("professional.setAudioSwitch.switchGateMinVal"));
        
        if (switchGateMinVal > swtichGate || switchGateMaxVal < swtichGate)
        {
            result.setErrCode(ErrInfo.PARAM_OUTOFRANGE_ERRORCODE);
            return result;
        }
        
        if (null == isSwitch)
        {
            result.setErrCode(ErrInfo.CONTROL_CODE_IS_NULL_ERRORCODE);
            return result;
        }
        
        if (1 == isSwitch)
        {
            enable = true;
        }
        else if (0 == isSwitch)
        {
            enable = false;
        }
        else
        {
            result.setErrCode(ErrInfo.CONTROL_CODE_IS_INCORRECT_ERRORCODE);
            return result;
        }
        
        // 准备Model
        HoldingConference conferenceAudioSwitch = new HoldingConference(confId);
        // 调用model
        result = conferenceAudioSwitch.setAudioSwitchEX(enable, swtichGate);
        
        return result;
    }
    
    public SDKErrorCode setBroadcastSiteEx(String confId, String siteUri, Integer isBroadcast)
        throws SDKException
    {
        SDKErrorCode result = new SDKErrorCode();
        
        if (StringUtils.isEmpty(confId))
        {
            result.setErrCode(ErrInfo.CONF_ID_ISNULL_ERRORCODE);
            return result;
        }
        if (!StringUtils.isUintNumber(confId))
        {
            result.setErrCode(ErrInfo.CONF_ID_IS_INVALID_ERRORCODE);
            return result;
        }
        if (StringUtils.isEmpty(siteUri))
        {
            result.setErrCode(ErrInfo.SITE_URI_ISNULL_ERRORCODE);
            return result;
        }
        if (null == isBroadcast)
        {
            result.setErrCode(ErrInfo.CONTROL_CODE_IS_NULL_ERRORCODE);
            return result;
        }
        
        if (0 == isBroadcast)
        {
            // 准备Model
            HoldingConference holdingConference = new HoldingConference(confId);
            // 调用model 开始广播
            result = holdingConference.setBroadcastSiteEx(siteUri);
        }
        else if (1 == isBroadcast)
        {
            // 准备Model
            HoldingConference holdingConference = new HoldingConference(confId);
            // 调用model 开始广播
            result = holdingConference.cancelBroadcastSiteEx(siteUri);
            //            result.setErrCode(ErrInfo.PARAM_IS_RESERVE_ERRORCODE);
        }
        else
        {
            result.setErrCode(ErrInfo.CONTROL_CODE_IS_INCORRECT_ERRORCODE);
        }
        
        return result;
    }
    
    public SDKErrorCode setSitesMuteEx(String confId, List<String> siteUris, Integer isMute)
        throws SDKException
    {
        SDKErrorCode result = new SDKErrorCode();
        boolean enable = false;
        if (StringUtils.isEmpty(confId))
        {
            result.setErrCode(ErrInfo.CONF_ID_ISNULL_ERRORCODE);
            return result;
        }
        if (!StringUtils.isUintNumber(confId))
        {
            result.setErrCode(ErrInfo.CONF_ID_IS_INVALID_ERRORCODE);
            return result;
        }
        if (ListUtils.isEmptyList(siteUris))
        {
            result.setErrCode(ErrInfo.SITE_URI_ISNULL_ERRORCODE);
            return result;
        }
        if (null == isMute)
        {
            result.setErrCode(ErrInfo.CONTROL_CODE_IS_NULL_ERRORCODE);
            return result;
        }
        if (0 == isMute)
        {
            enable = true;
        }
        else if (1 == isMute)
        {
            enable = false;
        }
        else
        {
            result.setErrCode(ErrInfo.CONTROL_CODE_IS_INCORRECT_ERRORCODE);
            return result;
        }
        
        // 准备Model
        SiteInConference siteInConference = new SiteInConference();
        siteInConference.setConfId(confId);
        siteInConference.setSiteUris(siteUris);
        // 调用model
        result = siteInConference.setSitesMuteEx(enable);
        
        return result;
    }
    
    public SDKErrorCode setBroadcastContinuousPresenceEx(String confId, Integer isBroadcast)
        throws SDKException
    {
        SDKErrorCode result = new SDKErrorCode();
        
        if (StringUtils.isEmpty(confId))
        {
            result.setErrCode(ErrInfo.CONF_ID_ISNULL_ERRORCODE);
            return result;
        }
        if (!StringUtils.isUintNumber(confId))
        {
            result.setErrCode(ErrInfo.CONF_ID_IS_INVALID_ERRORCODE);
            return result;
        }
        if (null == isBroadcast)
        {
            result.setErrCode(ErrInfo.CONTROL_CODE_IS_NULL_ERRORCODE);
            return result;
        }
        
        HoldingConference holdingConference = new HoldingConference(confId);
        SDKResult<ContinuousPresenceCapability> cpcapaResult = holdingConference.getContinuousPresenceInfoEx();
        
        // 判断会议是否支持多画面
        if (0 != cpcapaResult.getErrCode())
        {
            result.setErrCode(cpcapaResult.getErrCode());
            return result;
        }
        
        if (0 == isBroadcast)
        {
            result = holdingConference.setBroadcastContinuousPresenceEx();
        }
        else if (1 == isBroadcast)
        {
            // edit by gaolinfei
            // 用于取消广播多画面
            result = holdingConference.cancelBroadcastContinuousPresenceEx();
            // edit end
        }
        else
        {
            result.setErrCode(ErrInfo.CONTROL_CODE_IS_INCORRECT_ERRORCODE);
        }
        
        return result;
    }
    
    public SDKErrorCode setSitesQuietEx(String confId, List<String> siteUris, Integer isQuiet)
        throws SDKException
    {
        SDKErrorCode result = new SDKErrorCode();
        boolean enable = false;
        if (StringUtils.isEmpty(confId))
        {
            result.setErrCode(ErrInfo.CONF_ID_ISNULL_ERRORCODE);
            return result;
        }
        if (!StringUtils.isUintNumber(confId))
        {
            result.setErrCode(ErrInfo.CONF_ID_IS_INVALID_ERRORCODE);
            return result;
        }
        if (ListUtils.isEmptyList(siteUris))
        {
            result.setErrCode(ErrInfo.SITE_URI_ISNULL_ERRORCODE);
            return result;
        }
        if (null == isQuiet)
        {
            result.setErrCode(ErrInfo.CONTROL_CODE_IS_NULL_ERRORCODE);
            return result;
        }
        if (0 == isQuiet)
        {
            enable = true;
        }
        else if (1 == isQuiet)
        {
            enable = false;
        }
        else
        {
            result.setErrCode(ErrInfo.CONTROL_CODE_IS_INCORRECT_ERRORCODE);
            return result;
        }
        
        // 准备Model
        SiteInConference siteInConference = new SiteInConference();
        siteInConference.setConfId(confId);
        siteInConference.setSiteUris(siteUris);
        // 调用model
        result = siteInConference.setSitesQuietEx(enable);
        
        return result;
    }
    
    public SDKErrorCode setContinuousPresenceEx(String confId, ContinuousPresenceParam continuousPresenceParam)
        throws SDKException
    {
        SDKErrorCode result = new SDKErrorCode();
        if (StringUtils.isEmpty(confId))
        {
            result.setErrCode(ErrInfo.CONF_ID_ISNULL_ERRORCODE);
            return result;
        }
        if (!StringUtils.isUintNumber(confId))
        {
            result.setErrCode(ErrInfo.CONF_ID_IS_INVALID_ERRORCODE);
            return result;
        }
        
        // DTS2015012306064，target为可选的
        if (null == continuousPresenceParam || null == continuousPresenceParam.getPresenceMode())
        //    || null == continuousPresenceParam.getTarget())
        {
            result.setErrCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
            return result;
        }
        
        // R5C00屏蔽查询会场和资源的接口，不再需要，性能很差
        // 准备Model
        HoldingConference holdingConference = new HoldingConference(confId);
        
        // 调用model
        result = holdingConference.setContinuousPresenceEx(continuousPresenceParam);
        return result;
    }
    
    /** 
     * 启动会议直播功能
     *
     * @param confId 会议ID
     * @param videoSourceUri 视频源所在会场标识
     * @return SDKErrorCode 成功：0，失败返回相应错误码
     * @throws SDKException 用log记录Exception
     * @since eSDK TP V100R003C20
     */
    public SDKErrorCode startLiveBroadcastEx(String confId, String videoSourceUri)
        throws SDKException
    {
        return new RecordingConference().startLiveBroadcastEx(confId, videoSourceUri);
    }
    
    /** 
     * 停止会议直播功能
     *
     * @param confId 会议ID
     * @param videoSourceUri 视频源所在会场标识
     * @return SDKErrorCode 成功：0，失败返回相应错误码
     * @throws SDKException 用log记录Exception
     * @since eSDK TP V100R003C20
     */
    public SDKErrorCode stopLiveBroadcastEx(String confId, String videoSourceUri)
        throws SDKException
    {
        return new RecordingConference().stopLiveBroadcastEx(confId, videoSourceUri);
    }
    
    /** 
     * 启动录制
     *
     * @param confId 会议ID
     * @return SDKErrorCode 成功：0，失败返回相应错误码
     * @throws SDKException 用log记录Exception
     * @since eSDK TP V100R003C20
     */
    public SDKErrorCode startRecordingEx(String confId)
        throws SDKException
    {
        return new RecordingConference().startRecordingEx(confId);
    }
    
    /** 
     * 停止录制
     *
     * @param confId 会议ID
     * @return SDKErrorCode 成功：0，失败返回相应错误码
     * @throws SDKException 用log记录Exception
     * @since eSDK TP V100R003C20
     */
    public SDKErrorCode stopRecordingEx(String confId)
        throws SDKException
    {
        return new RecordingConference().stopRecordingEx(confId);
    }
    
    /** 
     * 查询历史会议的录播地址
     *
     * @param confIds 待查询录播地址的会议ID列表
     * @return SDKErrorCode 成功：0，失败返回相应错误码
     * @throws SDKException 用log记录Exception
     * @since eSDK TP V100R003C20
     */
    public SDKResult<RecordingConference> queryHistoryRecordAddrEx(List<String> confIds)
        throws SDKException
    {
        return new RecordingConference().queryHistoryRecordAddrEx(confIds);
    }
    
    /**
     * 指定会场发言（点名发言）
     * 
     * @param confId 指定的会议ID
     * @param siteUri 指定会场的Uri地址
     * @return SDKErrorCode 成功：0，失败返回相应错误码
     * @throws SDKException 用log记录Exception
     * @see eSDK TP V100R003C20
     */
    public SDKErrorCode setFloorEx(String confId, String siteUri)
        throws SDKException
    {
        return new RecordingConference().setFloorEx(confId, siteUri);
    }
    
    /** 
     * 设置会场音量值
     * 
     * @param confId 会议ID
     * @param siteVolumes 设置会场的音量值
     * @return SDKErrorCode 0：表示成功  其他数值：表示失败
     * @throws SDKException 用log记录Exception
     * @see eSDK TP V100R003C20
     */
    public SDKErrorCode setConfSiteVolumeEx(String confId, List<SiteVolume> siteVolumes)
        throws SDKException
    {
        return new RecordingConference().setConfSiteVolumeEx(confId, siteVolumes);
    }
    
    /** 
     * 选择准备录制的视频源
     * 
     * @param confId 会议ID
     * @param videoSourceUri 视频源所在会场标识
     * @return SDKErrorCode 0：表示成功  其他数值：表示失败
     * @throws SDKException 用log记录Exception
     * @see eSDK TP V100R003C20
     */
    public SDKErrorCode setRecordingVideoSourceEx(String confId, String videoSourceUri)
        throws SDKException
    {
        return new RecordingConference().setRecordingVideoSourceEx(confId, videoSourceUri);
    }
    
    /** 
     * 打开会议中会场的视频
     * 
     * @param confId 会议ID
     * @param siteUris 关闭视频会场的URI列表
     * @return SDKErrorCode 成功：0，失败返回相应错误码
     * @throws SDKException 用log记录Exception
     * @since eSDK TP V100R003C20
     */
    public SDKErrorCode displayConfSiteLocalVideoEx(String confId, List<String> siteUris)
        throws SDKException
    {
        return new RecordingConference().displayConfSiteLocalVideoEx(confId, siteUris);
    }
    
    /** 
     * 关闭会议中会场的视频
     * 
     * @param confId 会议ID
     * @param siteUris 关闭视频会场的URI列表
     * @return SDKErrorCode 成功：0，失败返回相应错误码
     * @throws SDKException 用log记录Exception
     * @since eSDK TP V100R003C20
     */
    public SDKErrorCode hideConfSiteLocalVideoEx(String confId, List<String> siteUris)
        throws SDKException
    {
        return new RecordingConference().hideConfSiteLocalVideoEx(confId, siteUris);
    }
    
    public SDKErrorCode unlockPresentationEx(String confId) throws SDKException
    {
        return new HoldingConference(confId).unlockPresentationEx();
    }

    public SDKErrorCode lockPresentationEx(String confId, String siteUri) throws SDKException
    {
        return new HoldingConference(confId).lockPresentationEx(siteUri);
    }
    
    public SDKErrorCode requestConfChairEx(String confId, String chairmanUri) throws SDKException
    {
        return new HoldingConference(confId).requestConfChairEx(chairmanUri);
    }
    
    public SDKErrorCode releaseConfChairEx(String confId) throws SDKException
    {
        return new HoldingConference(confId).releaseConfChairEx();
    }
}
